/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes;

import com.android1.amarena2d.commons.Tools;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.sprites.SharedSprite;
import com.android1.amarena2d.texture.AutoPackTextureAtlas;
import com.android1.amarena2d.texture.TextureDef;
import com.android1.amarena2d.texture.TextureOptions;
import com.android1.amarena2d.texture.Textures;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import org.amarena2d.examples.Example;

@Example(name = "AutoPackTextureAtlas", category = "Texture Atlas")
public class AutoPackTextureAtlasExampleScene extends Scene {

    public AutoPackTextureAtlasExampleScene() {

        engine.getRenderer().setClearColor(Color.WHITE);

        AutoPackTextureAtlas atlas = new AutoPackTextureAtlas(256, Pixmap.Format.RGB565);

        atlas.add("cloud", Textures.$("cloud1.png").getDef());

        //we randomly generate textures of diffenrent size an put them into the atlas
        for (int i = 0; i < 20; i++) {

            int s = (int) (20 + 30 * Tools.random0To1());
            Pixmap pixmap = engine.getGraphics().newPixmap(s, s, Pixmap.Format.RGB565);

            final float r = 1 * Tools.random0To1();
            final float g = 1 * Tools.random0To1();
            final float b = 1 * Tools.random0To1();
            pixmap.setColor(r, g, b, 1);
            pixmap.fill();
            pixmap.setColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, 1);
            pixmap.drawRectangle(0, 0, s - 1, s - 1);
            try {
                //if we set a tag, we can select them by name. otherwise they can only be selected by index.
                atlas.add("frame" + i, new TextureDef(pixmap, TextureOptions.NEAREST));
            } catch (Exception e) {
            }
        }


        //we init the atlas, so we can access the ManagedTexture directly. otherwise it would be null.
        atlas.init();

        //shows the full TextureFrame of the atlas
        SharedSprite sprite = new SharedSprite(atlas.getManagedTexture(), 10, 10);
        add(sprite);

        SharedSprite sprite2 = new SharedSprite(atlas.findTextureFrame("frame0"), 320, 250);
        add(sprite2);

        SharedSprite sprite3 = new SharedSprite(atlas.getTextureFrame(1), 320, 190);
        add(sprite3);

        SharedSprite sprite4 = new SharedSprite(atlas.getTextureFrame(2), 320, 130);
        add(sprite4);

        SharedSprite sprite5 = new SharedSprite(atlas.getTextureFrame(3), 320, 70);
        add(sprite5);

    }


}